import { fromJS } from "immutable";
import { getRankListApi } from "../../../api/request";


// constant 定义变量
export const CHANGE_RANK_LIST = 'home/rank/CHANGE_RANK_LIST';
export const CHANGE_LOADING = 'home/rank/CHANGE_LOADING';

// actionCreator
const changeRankList = (data) => {
    return {
        type: CHANGE_RANK_LIST,
        data: fromJS(data)
    }
};
const changeLoading = (data)  => {
    return{
        type: changeLoading,
        data
    }
};

// 请求api
export const getRankList = () => {
    return dispatch => {
        getRankListApi().then(data => {
            let list = data && data.list;
            dispatch(changeRankList(list));
            dispatch(changeLoading(false))
        })
    }
}

// rankReducer
const defaultState = fromJS({
    rankList: [],
    loading: true
});

const reducer = (state = defaultState, action) => {
    switch (action.type) {
        case CHANGE_RANK_LIST:
            return state.set('rankList', action.data);
        case CHANGE_LOADING:
            return state.set('loading', action.data);
        default:
            return state
    }
};

export { reducer };
